import Vue from "vue";
import {getInfo, isWhitelist, login, logout} from "@/api/testApi";
import {ACCESS_TOKEN, USER_INFO} from "@/store/mutation-types";
import {resetRouter} from "@/router";

const state = {
  showText:'hello world',
  token:'',
  userBaseInfo: {username:'未登录'},
  userId:'',
  userRoles:'',
  loginStatus:false,
}
const mutations = {
  SET_SHOW_TEXT(state,showText){
    state.showText=showText
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USER_BASE_INFO: (state, userBaseInfo) => {
    state.userBaseInfo = userBaseInfo
  },
  SET_USER_ROLES:(state,userRoles)=>{
    state.userRoles=userRoles
  },
  SET_USER_ID:(state,userId)=> {
    state.userId=userId
  },
  SET_LOGIN_STATUS:(state,loginStatus)=>{
    state.loginStatus=loginStatus
  }
}

const actions = {
  // 登录
  Login({commit}, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo).then(response => {
        if (response.code == '200') {
          const result = response.result
          commit('SET_TOKEN', result.token)
          commit('SET_LOGIN_STATUS',true)
          Vue.ls.set(ACCESS_TOKEN, result.token, 30 * 60 * 1000)

          resolve(response)
        } else {
          resolve(response)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  //根据token获取用户信息
  getInfo({commit, state}) {
    return new Promise((resolve, reject) => {
      let params = {token: Vue.ls.get(ACCESS_TOKEN)};
      getInfo(params).then(response => {
        console.log(response)
        if (response.code === 200) {
          const result = response.result
          const userInfo = result.userInfo
          commit('SET_USER_ID', userInfo.userId)
          commit('SET_USER_BASE_INFO', userInfo)
          commit('SET_USER_ROLES', userInfo.roles)
          commit('SET_LOGIN_STATUS',true)
          Vue.ls.set(USER_INFO, userInfo, 30 * 60 * 1000)
          resolve({roles: userInfo.roles})
        } else {
          resolve(response)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 登出
  Logout({commit, state}) {
    return new Promise((resolve) => {
      let logoutToken = state.token;
      //console.log('logoutToken: '+ logoutToken)
      logout(logoutToken).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_USER_BASE_INFO', {username:'未登录'})
        commit('SET_USER_ROLES', '')
        commit('SET_LOGIN_STATUS',false)
        Vue.ls.clear()
        resetRouter()
        resolve()
      }).catch(() => {
        resolve()
      })
    })
  },


  //网络白名单
  isWhitelist({commit,state}){
    return new Promise((resolve, reject) => {
      isWhitelist().then(response => {
        // console.log(response)
        resolve({viewCode: response.code})
      }).catch(error=>{
        reject(error)
      })
    })
  },
}
export default {
  state,
  mutations,
  actions,
  /**
   * 为true时两种方法调用action
   * 1....mapActions('空间名', [
   'action名'
   ])
   * 2.this.$store.dispatch('空间名/action名', this.$route)
   *
   *
   * 为false时 这样调用 this.$store.dispatch('action名', this.$route)，所以此时action名不能重复
   */
  namespaced: true,
}
